package com.macasa.panel.entity.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IServer;
import com.macasa.panel.entity.api.IService;

@Entity
@Table(name = "service")
@SequenceGenerator(name = "Service_SEQ", sequenceName = "ServiceIdSeq", initialValue = 1)
@Inheritance(strategy = InheritanceType.JOINED)
@Getter
@Setter
@ToString
public abstract class Service extends AbstractModel<Integer> implements IService {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 1809609183485369150L;

	@Id
	@Column(name = "service_id", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "Service_SEQ")
	private Integer				id;

	@OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.Server.class)
	@JoinColumn(name = "fk_hosted_on", nullable = false)
	@ForeignKey(name = "fk_service_01")
	private IServer				hostedOn;

	@Column(name = "name", nullable = false)
	private String				name;

	@Column(name = "available", nullable = false)
	private Boolean				available;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Service)) {
			return false;
		}

		final Service other = CastHelpers.cast(obj, Service.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.hostedOn, other.hostedOn) && Objects.equal(this.name, other.name)
				&& Objects.equal(this.available, other.available);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.hostedOn, this.name, this.available);
	}
}
